import request from '@/router/axios';
// import * as qs from 'qs'

const api = {
    appDetail: "/api/app/detail",
    worksheetGroupDetail: "/api/app/worksheet/group/get",
    addGroup: '/api/app/worksheet/group/save',
    updateGroup: '/api/app/worksheet/group/update',
    updateGroupSort: '/api/app/update-group-sort',
    updateWorksheetSort: '/api/app/worksheet/group/update-worksheet-sort',
    deleteGroup: '/api/app/worksheet/group/delete',
    allWorksheet: '/api/app/worksheet/info/listByAppId',
    updateApp: '/api/app/update',
    appList: '/api/Cgs/Portal/Visual/List?VisualType=App',
    appAuth: '/api/User/getAppAuth',
    appAuthV2: '/api/v2/User/getAppAuth',
    noticeCount: '/api/notice/count',
    noticeList: '/api/notice/list',
    noticeClear: '/api/notice/clear',
    taskDetail: '/api/automations/task/detail',
    updateTask: '/api/automations/task/update',
    taskCount: '/api/automations/task/count',
    taskTodoList: '/api/automations/task/todoList',
    taskHistory: '/api/automations/instance/history'
}

export function getAppDetail(id) {
    return new Promise((resolve, reject) => {
        request({
            url: api.appDetail + `?id=${id}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到应用数据"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getShareAppDetail(id) {
    return new Promise((resolve, reject) => {
        request({
            url: api.appDetail + `?id=${id}`,
            method: 'get',
            headers: {
              'X-Mode': 'share'
            }
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到应用数据"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getTaskDetail(id) {
    return new Promise((resolve, reject) => {
        request({
            url: api.taskDetail + `?id=${id}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到任务详情"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getAppList() {
    return new Promise((resolve, reject) => {
        request({
            url: api.appList,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到应用列表"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getAppAuth(visualId) {
    return new Promise((resolve, reject) => {
        request({
            url: api.appAuth + `?visualId=${visualId}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到应用权限"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getAppAuthV2(visualId) {
    return new Promise((resolve, reject) => {
        request({
            url: api.appAuthV2 + `?visualId=${visualId}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到应用权限"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getWorksheetGroupDetail(id) {
    return new Promise((resolve, reject) => {
        request({
            url: api.worksheetGroupDetail + `?id=${id}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到工作表数据"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function addWorksheetGroup(data) {
    return new Promise((resolve, reject) => {
        request({
            url: api.addGroup,
            method: 'post',
            data: data,
            headers: {
              'Content-Type': 'application/json'
            }
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("添加分组失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function updateWorksheetGroup(data) {
    return new Promise((resolve, reject) => {
        request({
            url: api.updateGroup,
            method: 'post',
            data: data,
            headers: {
              'Content-Type': 'application/json'
            }
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("分组重命名失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function updateWorkflowTask(data) {
    return new Promise((resolve, reject) => {
        request({
            url: api.updateTask,
            method: 'post',
            data: data,
            headers: {
              'Content-Type': 'application/json'
            }
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.success)
            }else{
                reject(new Error("更新任务失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function updateGroupSort(data) {
    return new Promise((resolve, reject) => {
        request({
            url: api.updateGroupSort,
            method: 'post',
            data: data,
            headers: {
              'Content-Type': 'application/json'
            }
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.success)
            }else{
                reject(new Error("分组移动失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function updateWorksheetSort(data) {
    return new Promise((resolve, reject) => {
        request({
            url: api.updateWorksheetSort,
            method: 'post',
            data: data,
            headers: {
              'Content-Type': 'application/json'
            }
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.success)
            }else{
                reject(new Error("工作表移动失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function deleteWorksheetGroup(params) {
    return new Promise((resolve, reject) => {
        request({
            url: api.deleteGroup,
            method: 'post',
            params: params
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("删除分组失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getAllWorksheet(appId) {
    return new Promise((resolve, reject) => {
        request({
            url: api.allWorksheet,
            method: 'get',
            params: {appId}
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未能获取所有表单"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function clearNotice(noticeType) {
    return new Promise((resolve, reject) => {
        request({
            url: api.noticeClear+`?NoticeType=${noticeType}`,
            method: 'post'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未清空消息"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getNoticeCount() {
    return new Promise((resolve, reject) => {
        request({
            url: api.noticeCount,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到消息数量"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getNoticeList({NoticeType, Current, Size}) {
    return new Promise((resolve, reject) => {
        request({
            url: `${api.noticeList}?NoticeType=${NoticeType}&Current=${Current}&Size=${Size}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到消息列表"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function updateApp(params) {
    return new Promise((resolve, reject) => {
        request({
            url: api.updateApp,
            method: 'post',
            data: params
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data)
            }else{
                reject(new Error("更新失败"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getTaskCount() {
    return new Promise((resolve, reject) => {
        request({
            url: api.taskCount,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到应用数据"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getTaskTodolist(type) {
    return new Promise((resolve, reject) => {
        request({
            url: api.taskTodoList + `?type=${type}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到应用数据"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}

export function getTaskHistory(nodeId, autoInstanceId) {
    return new Promise((resolve, reject) => {
        request({
            url: api.taskHistory + `?nodeId=${nodeId}` + `&autoInstanceId=${autoInstanceId}`,
            method: 'get'
        }).then((result) => {
            if(result?.data.success){
                resolve(result.data.data)
            }else{
                reject(new Error("未请求到应用数据"))
            }
        }).catch((err) => {
            reject(err)
        });
    })
}